package com.xinqing.test.query;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.feidee.fdcommon.authorization.AuthorizationVerify;
import com.feidee.fdcommon.authorization.model.AuthInfoModel;
import com.feidee.fdcommon.configuration.CustomConfiguration;
import com.feidee.fdcommon.model.StringResponse;
import com.feidee.fdcommon.util.HttpUtil;
import com.xinqing.test.modle.RequestTag;
import com.xinqing.test.modle.TagParam;
import org.apache.http.Consts;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:CHQ
 * @Date:2021/4/8 18:03
 * @Description
 */
public class TagService {
    public static void main(String args[]) throws Exception {

   /*     {
            "hbaseFlag": "1",
                "planTagMap": {
            "new": {
                "((userssj_user_vip_gold==0) && (userssj_user_vip_nor==0))": ["AD_LM_"]
            }
        },
              "productName": "mymoney",
                "redisFlag": "1",
                "requestId": "1401fa307f97f2842a0bde019252402f6a0",
                "serviceType": "2",
                "tagsName": ["userssj_user_vip_nor","userssj_user_vip_gold"],
            "udid": "ceb1eac464b6f103997fe1c1505019a2d55fefe9cadd508b1c7bc3adf80fdfa7a6887e9dad11f88ac49ca2d201d6f3cc",
                "userName": "beyo567@163.com"
        }
*/
        //String data = "{\"hbaseFlag\":\"1\",\"planTagMap\":\\{\"new\":\\{\"((userssj_user_vip_gold==0) && (userssj_user_vip_nor==0))\":[\"AD_LM_\"]\\}\\},\"productName\":\"mymoney\",\"redisFlag\":\"1\",\"requestId\":\"1452ee91baf7d3641aeac3fca39b0fb2049\",\"serviceType\":\"2\",\"tagsName\":[\"userssj_user_vip_nor\",\"userssj_user_vip_gold\"],\"udid\":\"androidId-dab59aa05c21a0d6\",\"userName\":\"u_10wwbz363u\"}";
        //"planTagMap":{"new":{"((userssj_user_vip_gold==0) && (userssj_user_vip_nor==0))":["AD_LM_"]}},"tagsName":["userssj_user_vip_nor","userssj_user_vip_gold"],

        RequestTag requestTag = new RequestTag();
        requestTag.setRequestId("777");
        requestTag.setHbaseFlag("1");
        Map<String, Map<String, List<String>>> bigMap = new HashMap<>();
        Map<String, List<String>> smallMap = new HashMap<>();
        ArrayList<String> list = new ArrayList<>();
        list.add("AD_LM_");
        smallMap.put("((userssj_user_vip_gold==0) && (userssj_user_vip_nor==0))", list);
        bigMap.put("new", smallMap);
        requestTag.setPlanTagMap(bigMap);
        requestTag.setProductName("mymoney");
        requestTag.setRedisFlag("1");
        requestTag.setServiceType("2");
        List<Object> list1 = new ArrayList<>();
        list1.add("userssj_user_vip_nor");
        list1.add("userssj_user_vip_gold");
        JSONArray jsonArray = new JSONArray(list1);
        requestTag.setTagsName(jsonArray);
        requestTag.setUdid("ceb1eac464b6f103997fe1c1505019a2d55fefe9cadd508b1c7bc3adf80fdfa7a6887e9dad11f88ac49ca2d201d6f3cc");
        requestTag.setUserName("beyo567@163.com");


        //接口需要传输的数据data
        String data = JSON.toJSONString(requestTag);

        //String data="{\"eng\":\"impala\",\"sql\":\"select 7788988891239\",\"key\":\"qkw@ct2m\",\"apt\":\"chenghuanqing\"}";

        //分配的AccessKeyId
        String accessKeyId = "data_team";
        //分配的AccessSecret
        String accessSecret = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ6+UYiQtt2OYfyfBF8e12kjd6nnF0Zl1s23f7l+eSl07xQxoxACSXyO3aq/tF7g0IiQZev4SF7ZA/oAOIYlYo0CAwEAAQ==";

        String url = "http://tg.feidee.com/ms/tag-service/batchTags";
        //调用鉴权工具包计算鉴权信息,data不存则传空
        AuthInfoModel authInfo = AuthorizationVerify.getAuthInfo(url, data, accessKeyId, accessSecret);
        //获取GMT时间+
        String date = authInfo.getDate();
        //获取加密之后的data，若传入data为空，则密文也为空
        String ciphertext = authInfo.getCiphertext();
        //获取认证信息
        String authorization = authInfo.getAuthorization();

        //构造请求
        HttpPost method = null;
        try {
            method = new HttpPost(url);
            method.setHeader("Date", date);
            method.setHeader("Authorization", authorization);
            method.setHeader("AuthVersion", "2");
            if (ciphertext != null) {
                //加密之后data通过http body传输
                StringEntity stringEntity = new StringEntity(ciphertext, Consts.UTF_8);
                stringEntity.setContentEncoding("UTF-8");
                stringEntity.setContentType("application/json");
                method.setEntity(stringEntity);
            }
            CustomConfiguration.setString("http.connection.socket.timeout", "50000");
            StringResponse response = HttpUtil.post(method);
            Integer statusCode = response.getStatusCode();
            System.out.println("statusCode:" + statusCode);
            // System.out.println(JSON.toJSONString(response));
            //System.out.println(response.toString());
            //  String responseBody = response.getResponseBody();
            // System.out.println("rslt:" + responseBody);
        } finally {
            if (method != null) {
                method.releaseConnection();
            }
        }


    }
}
